package com.example.huhaichao.music.fragment;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.huhaichao.music.adapter.ViewAdapter;
import com.example.huhaichao.music.database.DBService;
import com.example.huhaichao.music.util.MusicUtils;
import com.example.huhaichao.music.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by huhaichao on 2017/5/1.
 */

public class FavoriteMusic extends Fragment implements AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
    public Callbacks2 mCallbacks2;
    private List<String> listItems = new ArrayList<>(); // 要播放的喜爱列表
    private DBService dbService;
    private ViewAdapter viewAdapter;
    private static final String TAG = "FavoriteMusic";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_list, container, false);
        dbService = new DBService(getActivity());
        listItems = getListItems();
        MusicUtils.initMusicList2(listItems);//方便MusicService 播放
        ListView listview = (ListView) v.findViewById(R.id.musiclist); // 获取布局管理器中添加的ListView组件
        viewAdapter = new ViewAdapter(getActivity(), listItems);
        listview.setAdapter(viewAdapter); // 将适配器与ListView关联
        // 当单击列表项时播放音乐
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                Log.d(TAG, "onItemClick: " + position);
                mCallbacks2.onItemSelectedFavorite(position);
            }
        });
        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //如果activity没有实现Callbacks接口，抛出异常
        if (!(activity instanceof Callbacks2)) {
            throw new IllegalStateException("没有实现接口");
        }
        mCallbacks2 = (Callbacks2) activity;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    //添加、删除 更新ListView
    public void update(int operation, String url) {
        if (operation == 1)
            viewAdapter.add(url);
        if (operation == 2)
            viewAdapter.remove(url);
    }

    public interface Callbacks2 {
        void onItemSelectedFavorite(int id);
    }

    private List<String> getListItems() {
        List<String> listItems = new ArrayList<String>();
        Cursor cursor = dbService.myQuery("select * from Musics", null);
        if (cursor.moveToFirst()) {
            do {
                listItems.add(cursor.getString(cursor.getColumnIndex("musicUrl")));
            } while (cursor.moveToNext());
        }
        cursor.close();
        return listItems;
    }
}
